package com.bum.api.processor.keeng.html.bo;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;

import com.bum.api.bo.KeengGuruBO;
import com.bum.api.core.exp.om.Metas;
import com.bum.api.core.exp.om.Params;
import com.bum.api.core.html.HtmlModel;
import com.bum.api.core.html.HtmlRender;
import com.bum.api.processor.rss.html.bo.RssUrlHelper;
import com.bum.guru.exp.keeng.om.KeengAlbum;
import com.google.gson.Gson;

public class KeengHtmlViewBO {
	KeengGuruBO	keengGuruBO;

	HtmlRender	htmlRender;
	Gson		gson;
	int			maxResult	= 20;

	public String home(String path, Params params, Metas metas) {
		HtmlModel model = HtmlModel.get(metas);

		List<KeengAlbum> albums = keengGuruBO.listAlbums(0, maxResult);
		model.put("albums", albums);
		model.put("needHead", "1");
		if (albums.size() == maxResult) {
			model.put("moreUrl", "/app/keeng/albums/" + albums.get(maxResult - 1).getDay());
		}
		model.put("urlHelper", RssUrlHelper.get());

		return htmlRender.render("keeng/keeng-home", model);
	}

	public String albums(String path, Params params, Metas metas) {
		HtmlModel model = HtmlModel.get(metas);

		long fromMs = NumberUtils.toLong(path, 0);

		List<KeengAlbum> albums = keengGuruBO.listAlbums(fromMs, maxResult);
		model.put("albums", albums);
		if (fromMs == 0) {
			model.put("needHead", "1");
		}
		if (albums.size() == maxResult) {
			model.put("moreUrl", "/app/keeng/albums/" + albums.get(maxResult - 1).getDay());
		}
		model.put("urlHelper", RssUrlHelper.get());

		return htmlRender.render("keeng/keeng-albums", model);
	}

	public String album(String path, Params params, Metas metas) {
		HtmlModel model = HtmlModel.get(metas);

		String[] parts = StringUtils.split(path, "/");
		String id = parts[0];

		KeengAlbum album = keengGuruBO.getAlbum(id);
		model.put("album", album);

		return htmlRender.render("keeng/keeng-album", model);
	}

	public void setKeengGuruBO(KeengGuruBO keengGuruBO) {
		this.keengGuruBO = keengGuruBO;
	}

	public void setHtmlRender(HtmlRender htmlRender) {
		this.htmlRender = htmlRender;
	}
}
